<html>
<head></head>
<meta charset="utf-8">
<script type="text/javascript" src="jquery.min.js"></script>
<style type="text/css">
	.mylist{ clear: both; }
	.mylist .item{ clear: both; line-height: 150%;display: block;}
</style>
<body>
	说明：用来测试本地的 go串口插件 ：：对应的项目为：  serial-port-json-server ;
	<br><br>
	<script type="text/javascript">
		var sock = null;
		var wsuri =  "ws://localhost:8805/ws" ;//"ws://192.168.0.143:8989/ws";
		var nowPort = "";//"/dev/tty.wchusbserial14130";
		window.onload = function() {
			startSock();
		};

		function startSock(){
			console.log("window.onload:", wsuri );
			sock = new WebSocket(wsuri);
			sock.onopen = function() {
				console.log("connected to " , wsuri);
			}
			sock.onclose = function(e) {
				console.log("connection closed (" + e.code + ")");
			}
			sock.onmessage = function(e) {
				workBack(e);
			}
		};

		function send() {
			var msg = document.getElementById('message').value;
			sock.send(msg);
		};

		function workBack(e){
			console.log("------------------ workBack ----------")
			console.log(e);
			var data = e.data ;
			if( data.charAt(0) != "{"){
				return ;
			}
			var json = JSON.parse(data);
			console.log("json data:",json );
			var comm = json.c ;

			if(comm === "se.list"){
				var ls = JSON.parse(json.d) ;
				    var ss="";
				    console.log("SerialPorts=", ls );
				    for(var i=0;i< ls.length;i++){
				    	ss += '<div class=item ><a onclick="openPort(\''+ ls[i].p +'\')" >'+ ls[i].p +'-- open</a> -- <a onclick="closePort(\''+ ls[i].p +'\')" >close</a> -- state:'+ls[i].s +' </div>' ;
				    	//console.log(i+'-list:',ls[i]);
				    }
				    $("#mylist").html(ss);
				    return ;
				return ;
			}

			if(comm === "se.open"){
				console.log("SerialPorts open=", json.d );
				return ;
			}

			if(comm === "se.close"){
				console.log("SerialPorts close=", json.d );
				return ;
			}

			if(comm === "se.back"){
				console.log("SerialPorts back=", json.d );
				return ;
			}

			console.log("------------------ end ----------")
			//console.log(json);
		}



		function getList(){
			var msg = '{"c":"se.list" ,"p":"" ,"d":""}';
			sock.send(msg);
		}

		function openPort(name){
			nowPort = name ;
			var msg ='{"c":"se.open" ,"p":"'+ nowPort+'" ,"d":"52420b0113fa052a03e8c7"}';
			//var msg = 'open '+ name +' 115200 tinyg';
			sock.send(msg);
		}

		function closePort(name){
			var msg ='{"c":"se.close" ,"p":"'+ nowPort+'" ,"d":"52420b0113fa052a03e8c7"}';
			//var msg = 'close '+ name 
			sock.send(msg);
		}

		function testSound(){
			var msg ='{ "c":"se.write" ,"p":"'+ nowPort+'" ,"d":"52420b0113fa052a03e8c7" }';
			//var msg = 'send '+ nowPort+' HEX52420b0113fa052a03e8c7\n';
			//var msg = 'send /dev/tty.wchusbserial14230 HEX52420b0113fa052a03e8c7';
			sock.send(msg);
		}

		function testVersion(){
			var msg ='{ "c":"se.write" ,"p":"'+ nowPort+'" ,"d":"52420603019e" }';
			//var msg = 'send '+ nowPort+' HEX52420603019e\n';
			//var msg = 'send /dev/tty.wchusbserial14230 HEX52420b0113fa052a03e8c7';
			sock.send(msg);
		}

		function testDongle(){
			var msg ='{ "c":"se.write" ,"p":"'+ nowPort+'" ,"d":"4d4b060201a1" }';
			//var msg = 'send '+ nowPort+' HEX4d4b060201a1';
			//var msg = 'send /dev/tty.SLAB_USBtoUART HEX4d4b060201a1';
			sock.send(msg);
		}


		function uphex(ty){
			nowPort = "/dev/tty.wchusbserial14230" ;
			var ck1='avrdude -C /Users/user01/Desktop/hex/avrdude.conf -v -p atmega2560 -c wiring -P /dev/tty.wchusbserial14230 -b 115200 -D -U flash:w:/Users/user01/Desktop/hex/k1.hex:i';
			var ck2 ='avrdude -C /Users/user01/Desktop/hex/avrdude.conf -v -p atmega328p -c arduino -P /dev/cu.wchusbserial14230 -b 115200 -D -U flash:w:/Users/user01/Desktop/hex/k2.hex:i';
			var msg1 ='{ "c":"se.uphex" ,"p":"'+ nowPort+'" ,"d":"'+ ck1+'" }';
			var msg2 ='{ "c":"se.uphex" ,"p":"'+ nowPort+'" ,"d":"'+ ck2+'" }';
			if(ty ===1) {
				sock.send(msg1);
			}
			else if (ty===2){
				sock.send(msg2);
			}
		}

	</script>
	<h1>WebSocket :serial-port-json-server  </h1>
	<form>
		Message:
		<p>
			 <input id="message" style="width:80%;" type="text" value="list">
		</p>
	</form>

	<button onclick="send();">Send Message</button>

	<br><br><br>
	<a onclick="getList();">serialList</a> ------ 
	<a onclick="testSound();">RB发声音</a> ------ 
	<a onclick="testVersion();">RBVersion</a> --- 
	<a onclick="testDongle();">Dongle版本</a>
	<br>
	<a onclick="uphex(1);">uphex-k1</a> --- 
	<a onclick="uphex(2);">uphex-k2</a> --- 
<br><br>
	<div id="mylist" class="mylist">
	</div>
</body>

</html>